//
//  PurchaseHistoryViewController.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/3/23.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import RxDataSources
import RxSwift
import RxCocoa
import SnapKit
import Then

class PurchaseHistoryViewController: ViewController, LeftBarButtonItemable {
    let vm = PurchaseHistoryViewModel()
    var tableView: UITableView! = nil
    var dataSource: RxTableViewSectionedReloadDataSource<PurchaseHistorySection>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        bindUI()
    }
    
    func initUI() {
        
        title = "订单明细"
        setLeftItem()
        
        tableView = UITableView.init(frame: CGRect.zero, style: .plain).then({ (tv) in
            tv.register(PurchaseHistoryCell.classForCoder(), forCellReuseIdentifier: "PurchaseHistoryCell")
            tv.allowsSelection = false
            tv.tableFooterView = UIView()
            
            self.view.addSubview(tv)
            tv.snp.makeConstraints({ (maker) in
                maker.edges.equalTo(safeAreaInsets())
            })
        })
    }
    
    func bindUI() {
        dataSource = RxTableViewSectionedReloadDataSource.init(configureCell: { (ds, tv, ip, item) -> UITableViewCell in
            let cell = tv.dequeueReusableCell(withIdentifier: "PurchaseHistoryCell", for: ip) as! PurchaseHistoryCell
            cell.set(title: item.productName, date: item.createdAt, cost: item.cost)
            return cell
        })
        
        vm.vmData.asObservable().bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
        
        vm.requstCommand.onNext(true)
    }
}
